<template>
  <div class="cart-control">
    <!-- transition 实现过度效果 -->
    <transition name="move">
      <!-- 减号 -->
      <div class="cart-decrease" v-show="food.count > 0" @click.stop="decrease">
        <span class="inner icon-remove_circle_outline"></span>
      </div>
    </transition>
    <!-- 数量 -->
    <div class="cart-count" v-show="food.count > 0">{{ food.count }}</div>
    <!-- 加号 -->
    <div class="cart-add icon-add_circle" @click.stop="add"></div>
  </div>
</template>

<script>
export default {
  name: "cart-control",
  props: {
    food: {
      type: Object
    }
  },
  methods: {
    // 添加数量
    add(event) {
      // 如果商品中没有数量，就给商品添加数量
      if (!this.food.count) {
        //
        this.$set(this.food, "count", 1);
      } else {
        this.food.count++;
      }
      // 派发事件，并将按钮传递出去
      // goods中监听add事件
      this.$emit("add", event.target);
    },

    // 减数量
    decrease() {
      if (this.food.count > 0) {
        this.food.count--;
      }
    }
  }
};
</script>

<style lang="stylus" scoped>
@import "~common/stylus/variable"

.cart-control
  display: flex;
  align-items: center;
  // 减号
  .cart-decrease
    padding 0 6px
    opacity 1
    .inner
      line-height: 24px;
      font-size: $fontsize-large;
      color $color-blue
      transition: all 0.4s linear
      transform: rotate(0deg)
    // 激活状态
    &.move-enter-active, &.move-leave-active
      transition: all 0.4s linear
    // 激活过度效果
    &.move-enter, &.move-leave-active
      opacity: 0
      transform: translate3d(24px, 0, 0) rotate(180deg)

  // 数量
  .cart-count
    width 12px
    line-height: 24px;
    text-align: center;
    font-size: $fontsize-mini;
    color: $color-grey
  // 加号
  .cart-add
    display: inline-block;
    padding 0 6px
    line-height: 24px;
    font-size: $fontsize-large;
    color $color-blue
</style>
